//
//  LSPulldDownOptionsView.m
//  LookScore
//
//  Created by wr on 2018/1/31.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSPulldDownOptionsView.h"
#import "LSPullDownOptionsCell.h"
@interface LSPulldDownOptionsView()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)UITableView *tableView;


@end

@implementation LSPulldDownOptionsView

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


-(instancetype)initWithFrame:(CGRect)frame normalTitleColor:(UIColor *)normalTitleColor selectedTitleColor:(UIColor *)selectedTitleColor normalBackViewColor:(UIColor *)normalBackViewColor selectedBackViewColor:(UIColor *)selectedBackViewColor height:(CGFloat )height{
    self=[super initWithFrame:frame];
    if (self) {
        self.normalTitleColor=normalTitleColor;
        self.selectedTitleColor=selectedTitleColor;
        self.normalBackViewColor=normalBackViewColor;
        self.selectedBackViewColor=selectedBackViewColor;
        self.height=height;
        self.backgroundColor=[UIColor colorWithRed:0.f green:0.f blue:0.f alpha:.1];
         [self addContentView];
    }
    return self;
}

-(void)addContentView{

    self.modelArray=[NSMutableArray array];
    
    self.tableView.backgroundColor = LSTitleWhiteColor;
    [self addSubview:self.tableView];
    self.tableView.sd_layout.leftEqualToView(self).topEqualToView(self).rightEqualToView(self).heightIs(self.height);
    
    
}



#pragma mark 🔥 tableViewDelegate tableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 35;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LSPullDownOptionsCell *cell=[LSPullDownOptionsCell PullDownOptionsCellByTableView:tableView];
    
    cell.leagueLabel.text=self.modelArray[indexPath.row];

    if (self.selectedIndex==indexPath.row) {
        cell.leagueLabel.textColor=self.selectedTitleColor;
        cell.contentView.backgroundColor=self.selectedBackViewColor;
    }else{
        cell.leagueLabel.textColor=self.normalTitleColor;
        cell.contentView.backgroundColor=self.normalBackViewColor;
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    LSPullDownOptionsCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    cell.leagueLabel.textColor=self.selectedTitleColor;
    cell.contentView.backgroundColor=self.selectedBackViewColor;
    self.selectedIndex=indexPath.row;
    [self.tableView reloadData];
    [self hidden];
    
    if (self.cellClickBlock) {
        self.cellClickBlock(indexPath.row);
    }
    
}

-(void)setModelArray:(NSMutableArray *)modelArray{
    _modelArray=modelArray;
    [self.tableView reloadData];
}

-(void)setSelectedIndex:(NSInteger)selectedIndex{
    _selectedIndex=selectedIndex;
    
}

-(void)setHeight:(CGFloat)height{
    _height=height;
}


///**
// *  显示
// */
//- (void)show
//{
//
//
//    // 1.获得最上面的窗口
//    UIWindow *window;// = [[UIApplication sharedApplication].windows lastObject];
//    if ( @available(iOS 11.0, *)) {
//        window = [UIApplication sharedApplication].keyWindow;
//    }else{
//        window = [[UIApplication sharedApplication].windows lastObject];
//    }
//    [window addSubview:self];
//
//}
/**
 隐藏
 */

- (void)show{
    self.hidden=NO;
}
-(void)hidden{
    self.hidden=YES;
}


/**
 *  销毁
 */
- (void)dismiss
{
    [self removeFromSuperview];
    
}

-(void)dealloc{
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [self dismiss];
    [self hidden];

}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
